<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends CommonController {


    public function _initialize() {
        $params = array("get_category");
        parent::_initialize($params);
        $this->assign("now_module" ,"goods");
    }


    //分类列表页面
    public function index()
    {
        $categories = M('Category')->order('sort ASC')->select();
        $categorie_s = sortCate($categories);
        $this->assign('categories',$categorie_s);
        $this->display();
    }


    //添加分类页面
    public function add_cate()
    {
        if (IS_POST)
        {
            //计算下级分类的level
            $pid1 = to_int(I('post.parent_id_1'));
            $pid2 = to_int(I('post.parent_id_2'));
            if($pid2 != 0){
                $parent = M('category')->where( array('id' => $pid2) )->find();
                $add_data["level"] = $parent['level'] + 1;
                $add_data["pid"] = $pid2;
            }elseif ($pid1 != 0) {
                $parent = M('category')->where( array('id' => $pid1) )->find();
                $add_data["pid"] = $pid1;
                $add_data["level"] = $parent['level'] + 1;
            } else {
                $add_data["level"] = 1;
                $add_data["pid"] = 0;
            }

            $add_data["c_name"] = trim(I('post.c_name'));
            $add_data["img"] = trim(I('post.image'));
            $add_data["sort"] = to_int(I('post.sort'));
            M('category')->add($add_data);
            $this->success("添加成功" ,U('Category/index') ,1);

        } else {
            $categories = M('category')->where("status = 1 AND pid = 0")->order('sort ASC')->select();
            $this->assign('categories', $categories);
            $this->display();
        }
    }

    //修改分类
    public function edit()
    {
        if (IS_POST)
        {
            $c_name = trim(I('post.c_name'));;
            if(!$c_name){
                $this->error("分类名称不能为空！" ,U('Category/index') ,2);
            }
            //计算下级分类的level
            $pid1 = to_int(I('post.parent_id_1'));
            $pid2 = to_int(I('post.parent_id_2'));
            $cate_id = to_int(I('post.cate_id'));
            if($pid2 != 0){
                $parent = M('category')->where( array('id' => $pid2) )->find();
                $save_data["level"] = $parent['level'] + 1;
                $save_data["pid"] = $pid2;
            }elseif ($pid1 != 0) {
                $parent = M('category')->where( array('id' => $pid1) )->find();
                $save_data["pid"] = $pid1;
                $save_data["level"] = $parent['level'] + 1;
            } else {
                $save_data["level"] = 1;
                $save_data["pid"] = 0;
            }
            $save_data["c_name"] = $c_name;
            $save_data["img"] = trim(I('post.image'));
            $save_data["sort"] = to_int(I('post.sort'));
            M('category')->where("id = %d", array($cate_id))->save($save_data);
            $this->success("添加成功" ,U('Category/index') ,1);

        } else {
            $cate_id = to_int(I('get.id'));
            // 获取分类的详细信息
            $parents = array();
            $category = M('category')->where(array('id' => $cate_id))->find();
            if($category['level'] == 3){
                // 查询上级的上级
                $pid = M('category')->where(array('id' => $category['pid']))->getField("pid");
                $parents = M('category')->where(array('pid' => $pid))->select();
                $this->assign("p_id2" ,$category['pid']);
                $this->assign("p_id1" ,$pid);
            }elseif($category['level'] == 2){
                $this->assign("p_id2" ,0);
                $this->assign("p_id1" ,$category['pid']);
            }else{
                $this->assign("p_id2" ,0);
                $this->assign("p_id1" ,0);
            }
            $this->assign('parents', $parents);
            $this->assign('category', $category);
            $categories = M('category')->where("status = 1 AND pid = 0")->order('sort ASC')->select();
            $this->assign('categories', $categories);
            $this->display();
        }
    }

    //删除分类
    public function del()
    {
        //在删除前先查询是否有子类，有的话要先提示
        $category = M('Category')->where( array('pid' => I('post.id')) )->find();
        if ($category) {
            $this->ajaxReturn(-1);//有子类
        } else {
            //没有子类的时候，要查询是否已经被某个商品占用
            $good = M('Good')->where( array('cate'=>I('post.id')) )->find();
            if (!empty($good)) {
                $this->ajaxReturn(-2);//有商品占用
            } else {
                M('Category')->where( array('id'=>I('post.id')) )->delete();
                $this->ajaxReturn(true);
                $this->ajaxReturn(1);
            }
        }
    }

    // 异步读取二级分类
    public function get_category()
    {
        $pid = to_int(I("pid"));
        $html = "";
        $categorys = M("category")->where("pid = $pid AND status = 1")->order("sort ASC")->select();
        foreach($categorys as $k => $v)
            $html .= "<option value='{$v['id']}'>{$v['c_name']}</option>";
        exit($html);
    }

}